package com.lcy.mybatis.generator.plugin.element;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;

import java.util.Map;

/**
 * <p>创建时间：2017/5/4 17:15</p>
 *
 * @author 娄存银
 * @version 1.0
 */
public class DeleteByColumnsElement extends CustomerSqlElement {
    private static final String SQL = "delete from %s\n" +
            "        <where>\n" +
            "            <if test=\"map == null or map.size() == 0\">\n" +
            "                false\n" +
            "            </if>\n" +
            "            <if test=\"map != null and map.size &gt; 0\">\n" +
            "                <foreach collection=\"map\" index=\"column\" item=\"value\">\n" +
            "                    <if test=\"column != null and value != null\">\n" +
            "                        AND ${column} = #{value}\n" +
            "                    </if>\n" +
            "                    <if test=\"column != null and value == null\">\n" +
            "                        AND ${column} is null\n" +
            "                    </if>\n" +
            "                    <if test=\"map.size() == 1 and column == null\">\n" +
            "                        and false\n" +
            "                    </if>\n" +
            "                </foreach>\n" +
            "            </if>\n" +
            "        </where>";

    public DeleteByColumnsElement(IntrospectedTable table) {
        super(table);
    }


    @Override
    protected void initXmlElement() {
        xmlElement = deleteElement();
    }

    @Override
    protected void initMethod() {
        method = new Method(getId());

        method.setReturnType(TYPE_INTEGER);

        Parameter parameter = new Parameter(TYPE_STRING_OBJECT_MAP, MAP,annotation(MAP));
        method.addParameter(parameter);


        imports.add(TYPE_MAP);
    }

    @Override
    protected String getId() {
        return "deleteByColumns";
    }

    @Override
    protected String getSql() {
        return SQL;
    }
}
